package com.freeLearning.boot.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freeLearning.boot.common.util.TreeUtil;
import com.freeLearning.boot.system.mapper.RegionMapper;
import com.freeLearning.boot.system.model.entity.Region;
import com.freeLearning.boot.system.model.vo.RegionVO;
import com.freeLearning.boot.system.service.RegionService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {
    private final RegionMapper regionMapper;

    @Cacheable(value = "region", key = "1",sync = true)
    public List<RegionVO> getRegionList() {
        List<RegionVO> list = this.regionMapper.selectList(new QueryWrapper<Region>()).stream().map(item->{
            return BeanUtil.copyProperties(item, RegionVO.class);
        }).toList();
        List<RegionVO> regionVOS = TreeUtil.buildTreeByRecursion(list);
        return  regionVOS;
    }
}
